home *** CD-ROM | disk | FTP | other *** search
- On 23-mei-97 Joona I Palaste wrote:
- >I am thinking of programming an AMOS game with robots that you can
- >actually program in a BASIC-type language. Unfortunately I don't know
- >how to write a decent arithmetic evaluation routine in AMOS. It should
- >evaluate expressions in the correct order, e.g. "2+(3+4)*5" is 37. I've
- >only managed to write an evaluation routine which doesn't care about
- >calculation orders, e.g. "2+3*4" would be 20, not 14. I need your help
- >here! Write me a routine that reads an expression as a string variable
- >and stores the result in a number variable. If you do this I will
- >eventually include your name in the game's documentation. Thanks in
- >advance.
-
- Well, the way you do it now is easier to program and possibly faster than the
- way you want it, so why don't stick with it?
-
- But that wasn't what you asked.
-
- Why don't you use recursion? (Asuming that nobody uses more than, say, ten
- levels of brackets).
-
- So any time you encounter an open bracket, you call the same routine with the
- tail of the string, and a close bracket will return to the calling routine.
-
- Say:
- Procedure KALK(EXPR$,PTR)
- A$=Mid$(EXPR$,PTR,1)
- If A$="("
- KALK(EXPR$,PTR+1)
- RESULT=RESULT+Param
- End If
- If A$<>")"
- 'Do your evaluation here'
- End If
- End Proc[RESULT]
-
- Of course, this only solves one part of your problem. The rest could be solved
- by
- putting brackets around the expressions that need to be evaluated first.
-
- --
- Branko Collin . |. .
- collin@xs4all.nl . . || ...
- http://www.xs4all.nl/~collin . ....||| .. ..
-
-
-